<template>
  <div>
    <el-form
      ref="formRel"
      :model="form"
      label-width="120px"
      style="max-width: 800px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品">
            <el-input v-model="form.product" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量">
            <el-input v-model="form.num" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="客户名称">
            <el-input v-model="form.cname" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户报价">
            <el-input v-model="form.cprice" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="供应商">
            <el-input v-model="form.pname" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商报价">
            <el-input v-model="form.pprice" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="货代">
            <el-input v-model="form.hname" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="货代报价">
            <el-input v-model="form.hprice" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="销售">
            <el-input v-model="user.account" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预计利润">
            <el-input :value="estimatedProfit" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <div>
        <el-button type="primary" @click="savefrom">保存</el-button>
        <el-button type="default" @click="cls">取消</el-button>
      </div>

      <!-- Add other fields similarly -->
    </el-form>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { getUser } from "@/utils/session.js";
import { computed } from "vue";
import { save } from "@/api/audit.js";
const router = useRouter();
const estimatedProfit = computed(() => {
  const cprice = parseFloat(form.value.cprice) || 0;
  const hprice = parseFloat(form.value.hprice) || 0;
  const pprice = parseFloat(form.value.pprice) || 0;
  return cprice - hprice - pprice;
});
const form = ref({
  cname: "",
  pname: "",
  hname: "",
  cprice: 0,
  pprice: 0,
  hprice: 0,
  product: "",
  num: "",
  status: 0,
  saleid: "",
  result: "",
  sname: ""
  // Initialize other fields similarly
});
let user;

async function savefrom() {
  let res = await save(form.value);
  if (res.code === 200) {
    ElMessage({
      message: "保存成功",
      type: "success"
    });

    //刷新部门列表
    router.push("/order/index");
    // cls();
  }
}
function cls() {
  emits("close");
}

onBeforeMount(() => {
  user = getUser();
  console.log(user);
  form.value.saleid = user.id;
  // form.saleid=user.
});
</script>
